package cn.skyclass.controls
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Image;
	import mx.effects.Glow;
	
	[Bindable]
	public class GlowImage extends Image
	{
		public function GlowImage()
		{
			buttonMode = true;
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			
			initEffect();
		}

		public var glowEnabled:Boolean = true;
		
		private var _glowDistance:Number = 20.0;
		public function get glowDistance():Number{
			return _glowDistance;
		}
		public function set glowDistance(value:Number):void{
			_glowDistance = value;
			initEffect();
		}
		
		private var _glowInColor:uint = 0x00ff00;
		public function get glowInColor():uint{
			return _glowInColor;
		}
		public function set glowInColor(value:uint):void{
			_glowInColor = value;
			initEffect();
		}
		
		private var _glowOutColor:uint = 0x00ff00;
		public function get glowOutColor():uint{
			return _glowOutColor;
		}
		public function set glowOutColor(value:uint):void{
			_glowOutColor = value;
			initEffect();
		}
		
		private var glowIn:Glow;
		private var glowOut:Glow;
		
		private function initEffect():void{
			glowIn = new Glow();
			glowIn.duration = 300;
			glowIn.alphaFrom = 0.5;
			glowIn.alphaTo = 1.0;
			glowIn.blurXFrom = glowDistance;
			glowIn.blurXTo = 0.0;
			glowIn.blurYFrom = glowDistance;
			glowIn.blurYTo = 0.0;
			glowIn.color = glowInColor;
			
			glowOut = new Glow();
			glowOut.duration = 300;
			glowOut.alphaFrom = 1.0;
			glowOut.alphaTo = 0.5;
			glowOut.blurXFrom = 0.0;
			glowOut.blurXTo = glowDistance;
			glowOut.blurYFrom = 0.0;
			glowOut.blurYTo = glowDistance;
			glowOut.color = glowOutColor;
		}
		
		private function onMouseOver(e:Event):void{
			if(glowEnabled){
				glowIn.stop();
				glowOut.stop();
				glowOut.play([this]);
			}
		}
		
		private function onMouseOut(e:Event):void{
			if(glowEnabled){
				glowOut.stop();
				glowIn.stop();
				glowIn.play([this]);
			}
		}

	}
}